package com.dongdongshop.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.dongdongshop.data.Result;
import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.model.TbBrand;
import com.dongdongshop.model.TbBrandExample;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class BrandServiceImpl implements BrandService {

    @Resource
    private TbBrandMapper tbBrandMapper;

    @Override
    public List<BrandVo> brandList(BrandVo Vo) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(Vo.getFirstChar())) {
            criteria.andFirstCharEqualTo(Vo.getFirstChar());
        }

        if (StringUtils.isNotBlank(Vo.getName())) {
            criteria.andNameLike("%" + Vo.getName() + "%");
        }

        List<TbBrand> list = tbBrandMapper.selectByExample(example);
        return list.stream().map(brand -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(brand, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
    }


    //增加
    @Override
    public void addBrand( BrandVo brandVo) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(brandVo,brand);
        tbBrandMapper.insertSelective(brand);
    }



    //删除
    @Override
    public void daleteBrand(Long[] ids) {
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria = example.createCriteria();
        List<Long> list = Arrays.asList(ids);
        criteria.andIdIn(list);
        tbBrandMapper.deleteByExample(example);
    }


    //通过id查询
    @Override
    public BrandVo findById(Long id) {
        TbBrand brand = tbBrandMapper.selectByPrimaryKey(id);
        BrandVo brandVo = new BrandVo();
        BeanUtils.copyProperties(brand,brandVo);
        return brandVo;
    }



    //修改
    @Override
    public void updateThis(BrandVo brandVo) {
        TbBrand brand = new TbBrand();
        BeanUtils.copyProperties(brandVo,brand);
        tbBrandMapper.updateByPrimaryKeySelective(brand);
    }



    @Override
    public List<TbBrand> getbrand() {
        return tbBrandMapper.selectByExample(null);
    }

}
